/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.JList;
import org.bff.slimserver.musicobjects.SlimAlbum;

/**
 *
 * @author bfindeisen
 */
public class AlbumListCellRenderer extends MusicListCellRenderer {

    private static HashMap<SlimAlbum, AlbumCellPanelIface> albumPanels;
    private static HashMap<SlimAlbum, AlbumCellPanelIface> albumArtPanels;
    private JukeboxProperties jp;
    private AlbumList.ListType type;

    public AlbumListCellRenderer(AlbumList.ListType type) {
        super();
        this.type = type;
        albumPanels = new HashMap<SlimAlbum, AlbumCellPanelIface>();
        albumArtPanels = new HashMap<SlimAlbum, AlbumCellPanelIface>();
        jp = JukeboxProperties.getInstance();
    }

    /**
     * must clear for memory problems
     */
    public void clearPanels() {
        albumPanels = new HashMap<SlimAlbum, AlbumCellPanelIface>();
        albumArtPanels = new HashMap<SlimAlbum, AlbumCellPanelIface>();
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        AlbumCellPanelIface panel = null;

        if (value instanceof SlimAlbum) {
            SlimAlbum album = (SlimAlbum) value;
            //setText(album.getName());


            if (jp.isAlbumListArt()) {
                panel = albumArtPanels.get(album);
                if (panel == null) {
                    panel = new AlbumCellWithArtPanel(album, 1);
                    albumArtPanels.put(album, panel);
                }

            } else {
                panel = albumPanels.get(album);
                if (panel == null) {
                    panel = new AlbumCellPanel(album, list);
                    albumPanels.put(album, panel);
                }
            }
        }

        if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (getType() != null) {
                switch (getType()) {
                    case ARTIST:
                        panel.hideArtist();
                        panel.showYear();
                        break;
                    case YEAR:
                        panel.hideYear();
                        panel.showArtist();
                }
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }

            if (panel.getPanel().getPreferredSize().getHeight() > 1) {
                list.setFixedCellHeight((int) panel.getPanel().getPreferredSize().getHeight());
            }

        }

        setColors(list, index, isSelected);


        if (panel != null) {
            return panel.getPanel();
        } else {
            setText(value.toString());
            return this;
        }
    }

    /**
     * @return the type
     */
    public AlbumList.ListType getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(AlbumList.ListType type) {
        this.type = type;
    }
}
